package cart.controller;

import cart.service.ICartService;
import common.model.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private ICartService cartService;
    //添加购物车
    @PostMapping("/add")
    public Result addToCart(@RequestHeader("uid") Long uid, @RequestParam Long skuId, @RequestParam Integer number) {
        return cartService.addToCart(uid, skuId, number);
    }
    //移出购物车
    @DeleteMapping("/delete")
    public Result deleteFromCart(@RequestParam String id) {
        cartService.deleteFromCart(id);
        return Result.success("成功删除购物车");
    }
    //购物车列表
    @GetMapping("/list")
    public Result getCartList(@RequestHeader("uid") Long uid) {
        return cartService.getCartsByUid(uid);
    }

    //根据id集合获取购物车列表
    @PostMapping("/sublist")
    public Result getCartListByIds(@RequestParam Long uid, @RequestBody List<String> cartIds) {
        return cartService.getCartsByUidandCartIds(uid, cartIds);
    }

    //根据id集合删除购物车
    @PostMapping("/deleteList")
    public Result deleteCartList(@RequestParam Long uid, @RequestBody List<String> cartIds) {
        cartService.deleteCartByUidandCartIds(uid, cartIds);
        return Result.success("成功删除购物车");
    }

    //更新数量
    @PostMapping("/update")
    public Result updateNumber(@RequestParam String id, @RequestParam Integer quantity) {
        System.out.println(id + "~~" + quantity);
        cartService.updateNumber(id, quantity);
        return Result.success("成功更新购物车数量");
    }

    //后台
    //获取所有用户的购物车列表
    @GetMapping("/jiang/list")
    public Result getCartListByAdmin(
        @RequestParam Integer page,//页码
        @RequestParam Integer size,//每页数量
        @RequestParam String query//搜索关键词
        ) {
        return cartService.getCartListByAdmin(page, size, query);
    }

    //删除购物车项
    @PostMapping("/jiang/delete/{id}")
    public Result deleteCartItem(@PathVariable String id) {
        return cartService.deleteCartItem(id);
    }

    //根据用户id集合删除所有购物车项
    @PostMapping("/jiang/clear/{userId}")
    public Result deleteCartItemList(@PathVariable Long userId) {
        return cartService.deleteCartItemList(userId);
    }
}
